/*
 * similarity_test.cpp
 *
 *  Created on: 23/06/2011
 *      Author: Yotam and Gilaad
 */
#include "../similarity.h"
#include "mtmtest.h"
#include "../matrix.h"
#include <stdarg.h>

Matrix initMatrix( int rows, int columns, ... ) {
	Matrix result( rows, columns );

	va_list vl;
	va_start( vl, columns );

	for( int r = 1; r <= rows; ++r ) {
		for( int c = 1; c <= columns; ++c ) {
			double cell = va_arg(vl,double);
			result(r, c) = cell;
		}
	}
	va_end( vl );
	return result;
}

bool testSimilarity(){

	Matrix A = initMatrix(2,2,1.5,2.5,3.5,4.5);
	Matrix B = initMatrix(2,2,1.0,2.0,1.0,2.0);
	Matrix C = initMatrix(3,3,1.0,-2.2,3.5,0.33,2.1,22.0,-9.0,5.0,2.5);
	Matrix D = initMatrix(3,3,0.0,0.0,2.5,3.0,2.0,5.0,0.0,0.0,5.5);

	Similarity sim1(1, 100, A,B);
	Similarity sim2(2,  22, C,D);
	Matrix resultAB = initMatrix(2,2,-8.5,-11.5,8.5,11.5);
	Matrix resultCD = initMatrix(3,3,
	10.296052794186700, -5.740873476853130,  0.295978645048072,
	10.133295434884700, -5.660421661435050,  1.850577127462380,
	-3.000800810697240,  1.653698806198860,  2.864368867248330);

	ASSERT_NO_THROW(sim1.execute());
	Matrix AB = sim1.execute();
	ASSERT_EQUALS(AB == resultAB, true);
	ASSERT_NO_THROW(sim2.execute());
	Matrix CD = sim2.execute();
	ASSERT_EQUALS(CD == resultCD, true);

	Similarity sim3(3, 58, B,A);
	Similarity sim4(4, 69, D,C);
	Similarity sim5(5, 99, A,C);
	Matrix E = initMatrix(2,3,1.0,2.0,3.0,4.0,5.0,6.0);
	Matrix F = initMatrix(3,2,2.0,3.0,4.0,5.0,6.0,7.0);
	Similarity sim6(6, 45, E,A);
	Similarity sim7(7, 44, A,E);
	Similarity sim8(8, 43, E,F);
	ASSERT_THROW(Job::JobFailed,sim3.execute());
	ASSERT_THROW(Job::JobFailed,sim4.execute());
	ASSERT_THROW(Job::JobFailed,sim5.execute());
	ASSERT_THROW(Job::JobFailed,sim6.execute());
	ASSERT_THROW(Job::JobFailed,sim7.execute());
	ASSERT_THROW(Job::JobFailed,sim8.execute());

	return true;
}

int main() {
	RUN_TEST(testSimilarity);
	return 0;
}
